import React from 'react';
import {Select } from 'antd';
const Option = Select.Option;

class SCSelect extends React.Component {
    constructor(props) {
        super(props);
        console.log('props',props)
        const value = props.initialValue || {};
        this.state = {
            currency: value.currency || '',
            options:value.options||[],
        };
    }


    componentDidMount() {
        if(this.props.url){
            this.getStuGroup();
        }
        if(this.props.data){
            this.setState(Object.assign(this.state.options, {options: this.props.data}))
        }
    }

    getStuGroup = () => {
        this.props.url().then(response => { 
            if (response&&response.data) {
                if(this.props.transfromData){
                    response.data=this.props.transfromData(response.data)
                }
                this.setState(Object.assign(this.state.options, {options: response.data || []}))
            }
        })
    }
    
    // componentWillReceiveProps(nextProps) {
    //     // Should be a controlled component.
    //     if (!!nextProps.value) {
    //         const value = nextProps.value;
    //         this.setState(Object.assign(this.state.currency, {currency: value}))
    //     }
    // }
    // handleCurrencyChange = (currency) => {
    //     console.log('==========>',currency)
    //     if (!('value' in this.props)) {
    //         this.setState({ currency });
    //     }
    //     this.triggerChange({ currency });
    // }
    // triggerChange = (changedValue) => {
    //     const onChange = this.props.onChange;
    //     if (onChange) {
    //         onChange(changedValue);
    //     }
    // }


    render() {
        return (
                <Select
                    value={this.state.currency}
                    optionLabelProp="children" optionFilterProp="children"
                    allowClear={true}
                    placeholder={this.props.placeholder||'请选择'}
                    {...this.props}
                >
                    {
                        this.state.options.map((item,index) =>
                            {   
                                return <Option value={item.id} key={index}>{item.name}</Option>
                            }
                        )
                    }
                </Select>
        )
    }
}

export default SCSelect;